Skip to content

Fix Year 2038 Problem in Sntp_ConvertToUnixTime#104

Merged
kar-rahul-aws merged 20 commits intoFreeRTOS:mainfrom
kar-rahul-aws:new_fix_2038_issue
Apr 15, 2025
Merged

Fix Year 2038 Problem in Sntp_ConvertToUnixTime#104
kar-rahul-aws merged 20 commits intoFreeRTOS:mainfrom
kar-rahul-aws:new_fix_2038_issue

Conversation

@kar-rahul-aws
Copy link
Copy Markdown
Contributor

@kar-rahul-aws kar-rahul-aws commented Apr 10, 2025

Description

This SNTP function assumes 32-bit signed UNIX timestamps, but devices going into production now should probably be using unsigned 32-bit UNIX timestamps or wider signed 64-bit timestamps.

This PR updates the SNTP to Unix time conversion logic to support 64-bit Unix time (Y2038 compliance) while maintaining backward compatibility with legacy 32-bit systems.

Test Steps

Checklist:

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

https://t.corp.amazon.com/P204919465

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@tony-josi-aws
Copy link
Copy Markdown
Member

Shouldn't we update the sntpClient_SetTime function in the example_sntp_client_posix.c to use the updated version of Sntp_ConvertToUnixTime?

@kar-rahul-aws
Copy link
Copy Markdown
Contributor Author

The coreSNTP Windows Simulator demo will need to be changed here , will do that once these changes get approved.

@kar-rahul-aws kar-rahul-aws requested a review from cookpate April 11, 2025 17:16
cookpate
cookpate previously approved these changes Apr 11, 2025
* Sntp Time since Era 1 Epoch
*/
*pUnixTimeSecs = UNIX_TIME_SECS_AT_SNTP_ERA_1_SMALLEST_TIME + pSntpTime->seconds;
*pUnixTimeSecs = ( UnixTime_t ) ( UNIX_TIME_SECS_AT_SNTP_ERA_1_SMALLEST_TIME + pSntpTime->seconds );
Copy link
Copy Markdown
Member

@tony-josi-aws tony-josi-aws Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also update the seconds field of SntpTimestamp_t or cast it to UnixTime_t (if seconds with uint32_t itself doesn't overflow) before the addition takes place in order to make this change effective? Otherwise, I believe the addition should be happening as uint32_t and the overflow must have happened before the UnixTime_t cast of the result.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

if( pSntpTime->seconds >= SNTP_TIME_AT_UNIX_EPOCH_SECS )
{
*pUnixTimeSecs = pSntpTime->seconds - SNTP_TIME_AT_UNIX_EPOCH_SECS;
*pUnixTimeSecs = ( UnixTime_t ) ( pSntpTime->seconds - SNTP_TIME_AT_UNIX_EPOCH_SECS );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

@kar-rahul-aws kar-rahul-aws merged commit 941ad2f into FreeRTOS:main Apr 15, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants